'use strict';

var webpack = require("webpack");
var path = require("path");
var glob = require('glob')

//路径定义
var srcDir = path.resolve(process.cwd(), 'src'); //process.cwd() 返回运行当前脚本的工作目录的路径
var distDir = path.resolve(process.cwd(), 'dist');
var nodeModPath = path.resolve(__dirname, './node_modules'); //__dirname 脚本的位置
var pathMap = require('./src/pathmap.json');
var publicPath = '/';
//插件定义
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin


module.exports = function(options) {
    options = options || {}
    var debug = options.debug !== undefined ? options.debug : true;

    var plugins = [];

    plugins.push(
        new CommonsChunkPlugin({
            name: 'vendor',
            minChunks: Infinity
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery",
            "window.$": "jquery"
        }),
        new HtmlWebpackPlugin({
            title: 'Erp',
            template: './src/index.ejs',
            favicon: './src/img/fav.ico',
            // chunks : ['vendor','main'],
            minify: {
                collapseWhitespace: true,
                collapseBooleanAttributes: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true
            }
        }),
        new ExtractTextPlugin("[name].css", { allChunks: true, resolve: ['modules'] })
    );

    //config
    var config = {
        entry: {
            'vendor': ['jquery', 'bootstrap','whatwg-fetch'],
            main: './src/main.js'
        },
        output: {
            path: path.join(__dirname, "dist"),
            filename: "js/[name].js",
            // chunkFilename: '[chunkhash:8].chunk.js',
            publicPath: publicPath
        },
        module: {
            loaders: [
                { test: /\.vue$/, loader: 'vue' }, {
                    test: /\.js$/,
                    exclude: /(node_modules|bower_components)/,
                    loader: 'babel-loader',
                    query: {
                        // presets: ['es2015'],
                        // plugins: ['transform-runtime']
                    }
                }, {
                    test: /\.((woff2?|svg)(\?v=[0-9]\.[0-9]\.[0-9]))|(woff2?|svg|jpe?g|png|gif)$/,
                    loaders: [
                        //小于10KB的图片会自动转成dataUrl，
                        'url?limit=10000&name=img/[hash:8].[name].[ext]',
                        'image?{bypassOnDebug:true, progressive:true,optimizationLevel:3,pngquant:{quality:"65-80",speed:4}}'
                    ]
                }, {
                    test: /\.((ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9]))|(ttf|eot)$/,
                    loader: 'url?limit=10000&name=fonts/[hash:8].[name].[ext]'
                },
                { test: /\.(tpl|ejs)$/, loader: 'ejs' },
                { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") },
                { test: /\.scss$/, loader: ExtractTextPlugin.extract("style-loader", "css!sass?sourceMap") },

            ]
        },
        resolve: {
            extensions: ['', '.js', '.css', '.scss', '.tpl', '.png', '.jpg'],
            root: [srcDir, nodeModPath],
            alias: pathMap,
            publicPath: '/'
        },
        plugins: plugins,
        devtool: '#source-map',
        vue: {
            loaders: {
                css: ExtractTextPlugin.extract("css"),
                scss: ExtractTextPlugin.extract("css!sass"),
            }
        },
    }

    return config;
}
